'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 提升士兵的精锐等级
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var moment = require('moment');
var dataApi = require('../../../../libs/dataApi.js');
var Code = require('../../../../consts/code');
var buildService = require('../../../../services/buildService');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var soldierConf = require('../../../../config/soldierConf');
var buildType = require('../../../../consts/buildType');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var soldierService = require('../../../../services/soldierService.js');
var arenaService = require('../../../../services/arenaService.js');
var marqueeConf = require('../../../../config/marqueeConf.js');
var marqueeModel = require('../../../../models/marqueeModel.js');
var activityService = require('../../../../services/activityService.js');
var actType = require('../../../../consts/activityType.js');

var max_soldier_star = 5;

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var soldier_type = msg.soldier_type;
    if (!soldier_type) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }
    // jshint maxstatements:100
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'arena', 'soldier', 'task', 'daily_task', 'achieve', 'bag', 'build', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var soldier_model = player_manager.soldier;

                var bag_model = player_manager.bag;
                var soldier = soldier_model.getSoldier(soldier_type);

                // 是否存在这种士兵
                if (!soldier) {
                    return next(null, {
                        code: Code.SOLDIER_NOT_EXIST
                    });
                }

                // 判断满星
                if (soldier.getStar() >= max_soldier_star) {
                    return next(null, {
                        code: Code.SOLDIER_STAR_REACH_MAX
                    });
                }

                var soldier_star_conf = soldierConf.getSoldierStarConf(soldier_type, soldier.getStar() + 1);
                if (!soldier_star_conf) {
                    return next(null, {
                        code: Code.SOLDIER_STAR_ERROR
                    });
                }

                var soldier_type_conf = soldierConf.getSoldierTypeConf(soldier_type);
                if (!soldier_type_conf) {
                    return next(null, {
                        code: Code.SOLDIER_NOT_EXIST
                    });
                }

                // 扣除士兵碎片
                if (!bag_model.subProperty(soldier_type_conf.patch_id, soldier_star_conf.soldier_patch_cost)) {
                    return next(null, {
                        code: Code.USER_SOLDIER_PATCH_NOT_ENOUGH
                    });
                }

                // 士兵升星
                var old_soldier_star = soldier.getStar();
                soldier.upgradeStar();

                // 发送走马灯条件判断
                var itme_conf = marqueeConf.getConf(4);
                var item_list = marqueeConf.getMsgConf(4);
                var now_star_lv = soldier.getStar();

                if (!!soldier.getIsStarMsg(now_star_lv) && now_star_lv > old_soldier_star) {
                    _.some(item_list, function (value) {
                        if (value.value == now_star_lv) {

                            // 加入走马灯信息到数据库
                            var soldier_name = soldierConf.getSoldierStarConf(soldier_type, now_star_lv).name;
                            var str = value.format;
                            var reg = /name/;
                            var name_value = role_model.getName();
                            var text = str.replace(reg, name_value);
                            var regg = /soldier/;
                            var text1 = text.replace(regg, soldier_name);
                            var list = ({
                                sort_id: itme_conf.priority,
                                text: text1,
                                is_time: itme_conf.fixed_time,
                                times: itme_conf.times,
                                interval_time: itme_conf.time
                            });

                            marqueeModel.create(list, function (err, module) {

                            });
                            soldier.setIsStarMsg(now_star_lv);
                            return true;
                        }
                    });
                }

                // 更新战力
                soldierService.updateSoldierPower(player_manager);
                soldierService.refreshWallSoldierPower(player_manager);

                // 更新竞技场战力
                arenaService.uupdateArenaPower(player_manager, 1);
                arenaService.uupdateArenaPower(player_manager, 2);

                if (now_star_lv > old_soldier_star) {
                    activityService.push(role_id, actType.SOLDIER_STAR, player_manager);
                    taskService.addTaskSoldierStarAll(player_manager, soldier.getStar());

                    // 士兵星级个数统计
                    var soldier_list = soldier_model.getSoldierList();
                    var star_item = {};
                    _.each(soldier_list, function (item) {
                        if (!star_item[item.star]) {
                            star_item[item.star] = 1;
                        } else {
                            star_item[item.star]++;
                        }
                    });
                    _.each(star_item, function (value, key) {
                        taskService.addSoldierNumStarTotal(player_manager, +key, value);
                    });
                }

                // 加入任务统计
                if (soldier.getStar() == 5) {
                    taskService.addTaskSoldierStarFive(player_manager);
                }

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
